home *** CD-ROM | disk | FTP | other *** search
/ Ultimate Screensaver / Ultimate Screen Savers Collection (CMS Distributing) (1996).ISO / saver3 / ss14 / sshook.c < prev    next >
C/C++ Source or Header  |  1990-11-02  |  5KB  |  132 lines

  1. /****************************************************************************
  2. *
  3. *  FILE:          SSHOOK.C
  4. *
  5. *  DESCRIPTION:   Dynamic Link Library for Screen Saver.
  6. *                 These functions form a DLL for a Windows Keyboard Hook
  7. *                 function.
  8. *
  9. *                 This software is hereby placed in the public domain.
  10. *                 Use and abuse it at your own risk!
  11. *
  12. *
  13. *  AUTHOR:        Tom Wheeler
  14. *                 31294 Morlock
  15. *                 Livonia, Mich.  48152
  16. *                 [72037,1742]
  17. *
  18. *****************************************************************************
  19. *
  20. *     DATE                                DESCRIPTION
  21. *  ----------     -----------------------------------------------------------
  22. *   10/11/90      Initial Development
  23. *   11/02/90      Minor improvements to structure
  24. *
  25. ****************************************************************************/
  26.  
  27. #include <windows.h>
  28. #include "ss.h"
  29.  
  30. /******************************** VARIABLES ********************************/
  31.  
  32. HANDLE hInst = NULL;
  33. HANDLE hUserHandle = NULL;
  34. FARPROC lpfnKeyHook = NULL;
  35. FARPROC lpfnOldKeyHook = NULL;
  36.  
  37. /******************************** FUNCTIONS ********************************/
  38.  
  39. /****************************************************************************
  40. *
  41. *  int FAR PASCAL LibMain(HANDLE hInstance,WORD wDataSeg,WORD cbHeapSize,
  42. *                         LPSTR lpszCmdLine)
  43. *
  44. *  DESCRIPTION:   DLL Initialization function.  Called from LIBENTRY.ASM
  45. *                 which is the startup code for the DLL.
  46. *
  47. *  INPUT:         hInstance      - This modules instance Handle
  48. *                 wDataSeg       - This modules Data Segment
  49. *                 cbHeapSize     - Size of heap (if any)
  50. *                 lpszCmdLine    - Command line parameters
  51. *
  52. *  OUTPUT:        Returns 1 (TRUE) to indicate success
  53. *
  54. ****************************************************************************/
  55. int FAR PASCAL LibMain(HANDLE hInstance,WORD wDataSeg,WORD cbHeapSize,
  56.                        LPSTR lpszCmdLine)
  57. {
  58.    hInst = hInstance;               /* store instance Handle               */
  59.    if(cbHeapSize != 0)              /* if DLL data seg is MOVEABLE         */
  60.       UnlockData(0);
  61.    return(1);                       /* initialization successful           */
  62. }
  63.  
  64. /****************************************************************************
  65. *
  66. *  int FAR PASCAL WEP(int nParameter)
  67. *
  68. *  DESCRIPTION:   DLL Shutdown function.  Called when DLL is terminating.
  69. *
  70. *  INPUT:         nParameter     -  Termination Code
  71. *
  72. *  OUTPUT:        Returns 1 (TRUE) to indicate success
  73. *
  74. ****************************************************************************/
  75. int FAR PASCAL WEP(int nParameter)
  76. {
  77.    UnhookWindowsHook(WH_KEYBOARD,lpfnKeyHook);
  78.    FreeProcInstance(lpfnKeyHook);
  79.    return(1);
  80. }
  81.  
  82. /****************************************************************************
  83. *
  84. *  DWORD FAR PASCAL KeyboardHook(int iCode,WORD wParam,LONG lParam)
  85. *
  86. *  DESCRIPTION:   Keyboard Hook function
  87. *
  88. *  INPUT:         iCode       - Windows Hook Code
  89. *                 wParam      - Virtual Key Code
  90. *                 lParam      - Status Bits
  91. *
  92. *  OUTPUT:        Calls DefHookProc after processing key
  93. *
  94. ****************************************************************************/
  95. DWORD FAR PASCAL KeyboardHook(int iCode,WORD wParam,LONG lParam)
  96. {
  97.    if((HIWORD(lParam) & 0x8000) && (iCode == HC_ACTION))
  98.                                                    /* check if key is being
  99.                                                     * released and not a non-
  100.                                                     * process key          */
  101.       PostMessage(hUserHandle,WM_HOOKKEY,wParam,lParam);
  102.    return DefHookProc(iCode,wParam,lParam,(FARPROC FAR *)&lpfnOldKeyHook);
  103. }
  104.  
  105. /****************************************************************************
  106. *
  107. *  BOOL FAR PASCAL InitHook(HANDLE hWnd)
  108. *
  109. *  DESCRIPTION:   Externally callable function intended for initialization.
  110. *                 Allows this module to get a Handle to the user window and
  111. *                 installs the Keyboard filter hook.
  112. *
  113. *  INPUT:         hWnd        - Users window handle
  114. *
  115. *  OUTPUT:        Returns TRUE if successful, FALSE otherwise
  116. *
  117. ****************************************************************************/
  118. BOOL FAR PASCAL InitHook(HANDLE hWnd)
  119. {
  120.    if(!hUserHandle) {
  121.       hUserHandle = hWnd;
  122.       lpfnKeyHook = MakeProcInstance((FARPROC)KeyboardHook,hInst);
  123.       lpfnOldKeyHook = SetWindowsHook(WH_KEYBOARD,lpfnKeyHook);
  124.       return TRUE;
  125.    }
  126.    else {
  127.       MessageBox(hWnd,"SSHOOK.DLL already active!","SSHOOK",MB_ICONASTERISK |
  128.                  MB_OK);
  129.       return FALSE;
  130.    }
  131. }
  132.